package com.xiaoai.juc;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * 通过队列实现生产者消费者问题
 *
 * @Author
 * @Date 2021-09-27 22:59
 */
public class ProducerConsumer {
    private static BlockingQueue<String> queue =
            new ArrayBlockingQueue<String>(5);

    private static class Producer extends Thread{
        @Override
        public void run() {
            try {
                queue.put("product");
            }catch (Exception e){
                e.printStackTrace();
            }
            System.out.println("produce..");
        }
    }

    private  static class Consumer extends Thread{
        @Override
        public void run() {
            try {
                String product = queue.take();
            }catch (Exception e){
                e.printStackTrace();
            }
            System.out.println("comsumer..");
        }
    }

    public static void main(String[] args){
        for (int i = 0; i < 2; i++) {
            Producer producer = new Producer();
            producer.start();
        }

        for (int i = 0; i < 5; i++) {
            Consumer consumer = new Consumer();
            consumer.start();
        }

        for (int i = 0; i < 3; i++) {
            Producer producer = new Producer();
            producer.start();
        }
    }
}
